<?

$check_me_authed = "NO_OK";	

if(($jid = $_POST["radioview"]) || ($jid = $_POST["job_id_posted"]) ){
	
	unset($COracle);
	
	$COracle = new oracleDb;
  $COracle->init($_SESSION["ORCL"], 
                 $_SESSION["OPASS"],
                 $_SESSION["SID"],
                 _NO_COMMENT);
  
	if ($COracle->connect()) {
		
		$srcDbHdr			  = "SLS_ORDER_HDR";
    $srcDbDtl       = "SLS_ORDER_DTL";
    $srcDbDtlSub    = "SLS_ORDER_SUB_DTL";
    $srcMstCategory = "MST_MODEL_CAT";
    $srcMstGroup    = "MST_MODEL_GRP";
		
		$_SESSION["jid"]  = $_POST["radioview"];
		$jobId            = &$_SESSION["jid"];
		
		/**
		* Fetch global header
		* And check the status if it is valid
		* where STATUS = _NOT_ACTIVE
		*
		*/
		fetchLstDataHeader($COracle,
                       $jobId,
                       $srcDbHdr,
                       $nHeader,
                       $lstHeader);
                       
		if($lstHeader["STATUS"][0] == _NOT_ACTIVE ||
		   $lstHeader["STATUS"][0] == _CANCELED){
  		
  		/**
  		* Init the header vars
  		*
  		*/
  		$_POST["trDateEnt"]   = date("d-m-Y",
  		                             strtotime($lstHeader["TR_DATE"][0]));
  		$_POST["supplierEnt"] = $lstHeader["CUSTOMER"][0];
  		$_POST["currencyEnt"] = $lstHeader["CURRENCY"][0];
  		$_POST["rateEnt"]     = $lstHeader["RATE"][0];
  		$_POST["memoEnt"]     = $lstHeader["MEMO"][0];
  		
  		/**
  		* Init the detail
  		*
  		*/
  		fetchLstDataDetail($COracle,
                         $jobId,
                         $srcDbDtl,
                         $nDetail,
                         $lstDetail);
  		
  		$_POST["totQtyEnt"]     = $lstDetail["TOT_QTY"][0];
  		$_POST["totCvtQtyEnt"]  = $lstDetail["TOT_CVT_QTY"][0];
  		$_POST["grandTotEnt"]   = $lstDetail["GRAND_TOT"][0];
  		
  		
  		/**
  		* Init sub details
  		*
  		*/
  		fetchLstDataSubDetail($COracle,
                            $jobId,
                            $srcDbDtlSub,
                            $nSubDetail,
                            $lstSubDetail);
  		
  		for($iRow = 0; $iRow < $nSubDetail; $iRow++){
  		  
  		  /**
  		  * Since category and group were kept as integer
  		  * we need to reextract the integer become name
  		  *
  		  */
  		  fetchCatById($COracle,
  		               $lstSubDetail["ICAT"][$iRow],
  		               $srcMstCategory,
  		               $_POST["categoryEnt"][$iRow]);
  		  
  		  fetchgrpById($COracle,
    		             $lstSubDetail["IGROUP"][$iRow],
    		             $srcMstGroup,
    		             $_POST["groupEnt"][$iRow]);
  		  
  		  /**
  		  * Init the normal vars
  		  *
  		  */
  		  $_POST["modelEnt"][$iRow]   = $lstSubDetail["MODEL"][$iRow];
  		  $_POST["modelIdEnt"][$iRow] = $lstSubDetail["MODEL_ID"][$iRow];
  		  $_POST["unitEnt"][$iRow]    = $lstSubDetail["UNIT"][$iRow];
  		  $_POST["qtyEnt"][$iRow]     = $lstSubDetail["QTY"][$iRow];
  		  $_POST["cvtEnt"][$iRow]     = $lstSubDetail["CVT_TO"][$iRow];
  		  $_POST["cvtQtyEnt"][$iRow]  = $lstSubDetail["CVT_QTY"][$iRow];
  		  $_POST["priceEnt"][$iRow]   = $lstSubDetail["PRICE"][$iRow];
  		  $_POST["hrzTotEnt"][$iRow]  = $lstSubDetail["HRZ_TOT"][$iRow];
  		}
  		
  		/**
  		* Check if this tx isn't canceled
  		* YES: it is modify
  		* NO : Add new entry with this copy
  		*
  		*/
  		if($lstHeader["STATUS"][0] != _CANCELED){
  		  
  		  $_SESSION["modify_mode"]= "Y";
  		}else{
  		  
  		  $_SESSION["modify_mode"]= "N";
  		}
  		
  		$_SESSION["init_num"]   = $nSubDetail;
  		
  		$COracle->dc();
  		
  		require("entry_restore_me.php");
  	
  	}else{
  	  
  	  usrDie("Tx can't be modified, it has been approved");
  	}
	}else{
	  
	  usrDie("Error: Oracle connection failed");
	}
}else{
	
	usrDie("Error:Cant retrieved job_id");
}

?>